﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using TDUpdate;

namespace TDDraw
{
    public static class DrawManager
    {
        public static List<DrawComponent> DrawList { get { return DrawManager.drawList; } }
        public static SpriteBatch DefaultSpriteBatch { get; set; }
        public static GameTime GameTime { get { return DrawManager.gameTime; } }
        public static int ScreenWidth { get; set; }
        public static int ScreenHeight { get; set; }
        public static int TileWidth { get; set; }
        public static int TileHeight { get; set; }
        public static GraphicsDevice GraphicsDevice { get; set; }

        internal static Vector2 LevelOffset = new Vector2(0, 0);

        static List<DrawComponent> drawList = new List<DrawComponent>();
        static GameTime gameTime;

        public static void Draw(GameTime gameTime)
        {
            DrawManager.gameTime = gameTime;

            DefaultSpriteBatch.Begin();
            //TODO: Implement a DrawOrder for DrawComponents (and InnerObjects?)
            foreach (DrawComponent component in DrawManager.DrawList)
            {
                for (int i = 0; i < component.Texture.Count(); i++)
                {
                    DrawManager.DefaultSpriteBatch.Draw(component.Texture[i], component.DestinationRectangle[i], Color.White);
                }
            }
            DefaultSpriteBatch.End();
        }

        public static void UpdateComponentList_Changed(object sender, UpdateComponentCollectionChangedEventArgs e)
        {
            string className = (e.ChangedItem.GetType().ToString().Split(new char[] { '.' }, 2))[1];
            string drawClassName = "TDDraw." + className;
            try
            {
                Type t = Type.GetType(drawClassName);
                drawList.Add((DrawComponent)Activator.CreateInstance(t, new object[] { e.ChangedItem }));
            }
            catch (NullReferenceException)
            {
                Console.WriteLine("Warning: Die DrawComponent: " + drawClassName + " konnte nicht gefunden werden.");
            }
            catch (MissingMethodException)
            {
                Console.WriteLine("Warning: Es wurde in " + drawClassName + " kein Konstruktor gefunden, der nur den Typ " + e.ChangedItem.GetType().ToString() + "erwartet.");
            }
        }

        public static List<ActionHappendResult> ActionHappend(int x, int y)
        {
            Rectangle target = new Rectangle(x, y, 1, 1);
            List<ActionHappendResult> hits = new List<ActionHappendResult>();

            foreach (DrawComponent component in DrawManager.DrawList)
            {
                for (int i = 0; i < component.DestinationRectangle.Count(); i++)
                {
                    //TODO: Hier sind auch noch Dinge drin, auf die nie geklickt werden kann/soll; z.B. die Maus oder das Auswahl-Rechteck in der Klasse Level.
                    if (target.Intersects(component.DestinationRectangle[i]))
                    {
                        hits.Add(new ActionHappendResult(component, component.GetInnerObject(i)));
                    }
                }
            }

            return hits;
        }
    }

    public class ActionHappendResult
    {
        public readonly object innerObject;
        public readonly object sender;

        public ActionHappendResult(object s, object obj)
        {
            this.innerObject = obj;
            this.sender = s;
        }
    }
}
